---
title: 'Reproducible code: Section 4 and Online appendix III'
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Introduction

This document includes the reproducible code to produce the outputs presented in Section 4 of the paper and in the Online appendix III.

```{r}
# Data preparation
library(ei.Datasets)
data <- merge_small_options(ei_NZ_2017, 
                            min.party = 3, 
                            min.candidate = 3)
district <- which(data$District == "Northland")
cruzada <- data$District_cross_percentages[[district]][, -1]
party.margins <- data$Votes_to_parties[[district]]
candidate.margins <- data$Votes_to_candidates[[district]]
party.margins <- party.margins[, -c(1, 2)] 
candidate.margins <- candidate.margins[, -c(1, 2)]
 
# Transfer matrix estimation
library(ecolRxC)
set.seed(123)
transitions <- ecolRxC(party.margins, candidate.margins, confidence = 0.95, B = 100)

# Save of the limits of the confidence intervals
write.table(round(transitions$VTM.lower*100, 1), "example_MT_lower.csv", sep = ";")
write.table(round(transitions$VTM.upper*100, 1), "example_MT_upper.csv", sep = ";")


# Transfer matrix plot
 plot(transitions, size.labels = 3.5, size.numbers = 8, size.margins = 5)

```
